package com.example.keshe0337.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.keshe0337.R;
import com.example.keshe0337.entity.ParkingRecord;
import com.example.keshe0337.entity.ParkingSpace;
import com.example.keshe0337.utils.TimeConverters;

import java.util.List;

public class ParkingRecordAdapter extends ArrayAdapter<ParkingRecord>  {

    private final Context context;
    private final List<ParkingRecord> parkingRecords;
    public ParkingRecordAdapter(Context context, List<ParkingRecord> parkingRecords) {
        super(context, 0, parkingRecords);
        this.context = context;
        this.parkingRecords = parkingRecords;
    }
    TextView parkingNumberText,startTimeText,totalTimeText,feeText;

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_record_item, parent, false);
        }
        ParkingRecord parkingRecord = parkingRecords.get(position); //若当前停车位信息为空直接跳过封
        if (parkingRecord == null) {
            return convertView;
        }

        parkingNumberText = convertView.findViewById(R.id.textParkingNumber);
        startTimeText = convertView.findViewById(R.id.textStartTime);
        feeText = convertView.findViewById(R.id.textFee);

        parkingNumberText.setText(parkingRecord.getSpaceNum());
        startTimeText.setText(TimeConverters.format(parkingRecord.getStartTime()));
        feeText.setText(parkingRecord.getFee()+" ￥");

        return convertView;
    }

    public void update(List<ParkingRecord> newData) {
        clear();           // 清空旧数据
        addAll(newData);   // 添加新数据
    }
}
